/**
 * 
 */
package edu.uta.ui;

import java.util.ArrayList;

import edu.uta.beans.ZipDataBean;
import edu.uta.parsers.XMLParser;

import edu.uta.ui.R;
import android.app.Activity;
import android.graphics.Color;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TableRow.LayoutParams;
import android.widget.TextView;
/**
 *  @author fall 2011 team5
 *  
 *       Activity to show the AQI levels when zipcode is entered  
 *       References  http://en.androidwiki.com/wiki/Dynamically_adding_rows_to_TableLayout 
 *        http://www.warriorpoint.com/blog/2009/07/01/android-creating-tablerow-  
 *        rows-inside-a-tablelayout-programatically/
 */
public class AQIByZipCode extends Activity{
	
	/**
	 * @uml.property  name="reportingArea"
	 */
	String reportingArea="";
	/**
	 * @uml.property  name="checkDate"
	 */
	String checkDate="";
	/**
	 * @uml.property  name="drawable"
	 * @uml.associationEnd  
	 */
	Drawable drawable;
	/**
	 * @uml.property  name="dataListSize"
	 */
	int dataListSize;
	
	int count=0;

	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.show_aqi_data);
		//linking the button variable to the corresponding resource id in the layout xml
		Button backButton = (Button) findViewById(R.id.BackButton2);	
		//set anonymous on click listener to the "Back" button
		backButton.setOnClickListener(new OnClickListener()
		{
			public void onClick(View v)
			{
				/*
				 * when back button is clicked show zipcode form view that
				 *  allows the user to enter the zipcode
				 */
				/*Intent intent = new Intent(v.getContext(), ZipActivity.class); 
				ZipActivity parentActivity = (ZipActivity)getParent();*/
				
				ZipActivity.group.back();
			}
		});


	
		/*
		 * extract values from the bundle
		 * value of zipcode is passed from zipactivity
		 */
		Bundle bundle = this.getIntent().getExtras();
		String text = bundle.getString("zipvalue");
		
		/* Find Tablelayout defined in main.xml */
		TableLayout table = (TableLayout)findViewById(R.id.myTableLayout);
		
		/*
		 * getting the AQI levels as ArrayList of beans for the entered
		 * zipcode by calling
		 * getAQIByZipCode() and passing zipcode as the input parameter
		 */
		ArrayList<ZipDataBean> zipDataList = XMLParser.getAQIByZipCode(text);
		dataListSize=zipDataList.size();
		//check if data was returned from getAQIByZipCode() method
		if(dataListSize>0)
		{
			//if data was returned display it using table layout
			//generating the rows dynamically depending on the size zipDataList
			
			//creating the first row which displays the forecast area
			TableRow row1 = new TableRow(this);
			
			//textview header1 displays the reporting area information
			TextView header1 = new TextView(this);
			header1.setText("AQI Levels for  "+zipDataList.get(0).getReportingArea()+" Area");
			header1.setTextColor(Color.BLACK);
			/*for the UI to be consistent even there are very few records
			 * text view font sizes are changed 
			 */
			/*if(dataListSize<7)
			{
				header1.setTextSize(15);
			}
			else{
				header1.setTextSize(12);
			}*/
			
			header1.setTextSize(15);
			//area info is italicized
			header1.setTypeface(null, Typeface.ITALIC);
			//width & height of header1 are set using Layout parameter constants	
			header1.setLayoutParams(new LayoutParams(
					LayoutParams.FILL_PARENT,
					LayoutParams.FILL_PARENT));
			//header1 is added to row
			row1.addView(header1);
			row1.setPadding(0, 2, 0, 2);
			//row added to table and width & height of row
			//are set using Layout parameter constants
			table.addView(row1,new TableLayout.LayoutParams(
					LayoutParams.FILL_PARENT,
					LayoutParams.FILL_PARENT));
			//iterating over the zipdatalist
			for(ZipDataBean dataBean:zipDataList)
			{
				/*checking the date attribute in the bean with variable checkdate
				*to analyze if data corresponds to same day 
				*if it is the same date , addition of the date row is skipped
				*else new date row is added
				*/
				if(!dataBean.getDateForecast().equals(checkDate))
				{	//if it is a different date
					//create a new date row to display the date
					TableRow row2 = new TableRow(this);
					TextView header2 = new TextView(this);
					//if date is today's date highlight it
					if(dataBean.today){
					header2.setText("  Today  "+dataBean.getDateForecast());
					header2.setTextColor(Color.BLACK);
					}
					else
					{
						count=1;
						header2.setText("  Forecast  for  "+dataBean.getDateForecast());
						header2.setTextColor(Color.DKGRAY);
					}
					//assign date from the current iteration to checkDate
					checkDate=dataBean.getDateForecast();
					/*for the UI to be consistent even there are very few records
					 * text view font sizes are changed 
					 */
					/*if(dataListSize<7)
					{
						header2.setTextSize(14);
					}
					else{
						header2.setTextSize(13);
					}*/
					
					header2.setTextSize(14);
					header2.setTypeface(null, Typeface.BOLD_ITALIC);
					header2.setLayoutParams(new LayoutParams(
							LayoutParams.FILL_PARENT,
							LayoutParams.FILL_PARENT));
					//adding date text view to the row
					row2.addView(header2);
					row2.setPadding(0, 3, 0, 3);
					//adding date row to the table
					table.addView(row2,new TableLayout.LayoutParams(
							LayoutParams.FILL_PARENT,
							LayoutParams.FILL_PARENT));

					
				}
				
				// new row for displaying AQI details
				
					TableRow row = new TableRow(this);
				//text view to set AQI info	
					TextView data = new TextView(this);
					data.setText("      "+dataBean.getParameterName()+ "      "+
							dataBean.getAQI()+"      "+dataBean.getCategoryName()+ "      ");
					data.setTextColor(Color.DKGRAY);
					
					if(count==0)
					{
						data.setTextColor(Color.BLACK);
					}
					data.setTextSize(16);
					data.setTypeface(null, Typeface.BOLD);
					
					data.setLayoutParams(new LayoutParams(
							LayoutParams.FILL_PARENT,
							LayoutParams.FILL_PARENT));
					//image view to display the color code
					ImageView img = new ImageView(this);
					String category = dataBean.getCategoryName();
					//setting drawable to the corresponding color code image 
					//depending on the category
					if(category.contains("Good"))
		        	{
		        		 drawable = this.getResources().getDrawable(R.drawable.aqi1circle);
		        		 
		        	}
		        	else if(category.contains("Moderate"))
		        	{
		        		 drawable = this.getResources().getDrawable(R.drawable.aqi2circle);
		        		 
		        	}
		        	else if(category.contains("UnhealthySG"))
		        	{
		        		drawable = this.getResources().getDrawable(R.drawable.aqi3circle);
		        		
		        	}
		        	else if(category.contains("Unhealthy"))
		        	{
		        		 drawable = this.getResources().getDrawable(R.drawable.aqi4circle);
		        		
		        	}
		        	else if(category.contains("Very Unhealthy"))
		        	{
		        		 drawable = this.getResources().getDrawable(R.drawable.aqi5circle);
		        		
		        	}
		        	else if(category.contains("Hazardous"))
		        	{
		        		 drawable = this.getResources().getDrawable(R.drawable.aqi6circle);
		        		 
		        	}
		        	else
		        	{
		        		 drawable = this.getResources().getDrawable(R.drawable.aqi0circle);
		        		
		        	}
					//set image to image view
					img.setImageDrawable(drawable);
					
					img.setLayoutParams(new LayoutParams(
							LayoutParams.WRAP_CONTENT,
							LayoutParams.WRAP_CONTENT));
					//add data and image to the same row
					row.addView(data);
					row.addView(img);
					//add row to table					
					table.addView(row,new TableLayout.LayoutParams(
							LayoutParams.FILL_PARENT,
							LayoutParams.FILL_PARENT));

			}
			
			if(dataListSize<5)
			{
				for(int i=0;i<5;i++)
				{
					TableRow noDataRow = new TableRow(this);	
					TextView noData = new TextView(this);
					noData.setText("\n   " );
					noData.setTextColor(Color.DKGRAY);
					noData.setTextSize(18);
					noData.setTypeface(null, Typeface.BOLD);
					noDataRow.addView(noData);
					table.addView(noDataRow,new TableLayout.LayoutParams(
							LayoutParams.FILL_PARENT,
							LayoutParams.FILL_PARENT));
				}
			}
			
		}
	
		else
		{//when no data is available for the given zip code
			TableRow noDataRow = new TableRow(this);
			TextView noData = new TextView(this);
			noData.setText("\n     Sorry no data is available\n" +
					"     corresponding to the zip code\n     " +text);
			noData.setTextColor(Color.BLACK);
			noData.setTextSize(18);
			noData.setTypeface(null, Typeface.BOLD);
			noDataRow.addView(noData);
			table.addView(noDataRow,new TableLayout.LayoutParams(
					LayoutParams.FILL_PARENT,
					LayoutParams.FILL_PARENT));
			
			for(int i=0;i<5;i++)
			{
				TableRow noDataRow1 = new TableRow(this);
				TextView noData1 = new TextView(this);
				noData1.setText("\n   " );
				noData1.setTextColor(Color.BLACK);
				noData1.setTextSize(18);
				noData1.setTypeface(null, Typeface.BOLD);
				noDataRow1.addView(noData1);
				table.addView(noDataRow1,new TableLayout.LayoutParams(
						LayoutParams.FILL_PARENT,
						LayoutParams.FILL_PARENT));
			}

		}
	}


	



}
